package com.schoolET.controller.wechat;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.json.JSONUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.schoolET.entity.dto.WechatInfoDTO;
import com.schoolET.entity.dto.WechatTokenDTO;
import com.schoolET.entity.vo.EditWechatInfoVO;
import com.schoolET.http.WechatHttps;
import com.schoolET.result.Result;
import com.schoolET.service.wechat.WechatClientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author: lin
 * @className: WechatLoginController
 * @createTime: 2024-06-05  19:53
 * @description: 用户登录控制器
 */
@Api(tags = "用户登录")
@RestController
@RequestMapping("/schoolET/wx")
@Slf4j
public class WechatLoginController {

    @Resource
    private WechatClientService wechatClientService;

    @Resource
    private WechatHttps wechatHttps;

    @ApiOperationSupport(author = "lin")
    @ApiOperation(value = "用户登录")
    @PostMapping("/login")
    public Result wechatLogin(String code) {
        log.info("code:{}",code);
        String str =  wechatHttps.wechatHttpUrl("wx13cf4742a33d0eca",
                "137d0a27ab00e9e6cc51d34f60ad9499",
                code,
                "authorization_code");
        //将str转换成map集合
        log.info("str:{}",str);
        Map map = JSONUtil.toBean(str, Map.class);
        if (map.get("openid") == null) {
            return Result.error().msg("登录失败");
        }
        String openId = (String) map.get("openid");
        boolean b = wechatClientService.getOpenId(openId);
        StpUtil.login(openId);
        String tokenValue = StpUtil.getTokenValue();
        StpUtil.setTokenValue(tokenValue);
        WechatTokenDTO wechatTokenDTO = new WechatTokenDTO();
        wechatTokenDTO.setToken(tokenValue);
        wechatTokenDTO.setFlag(b);
        return Result.success(wechatTokenDTO).msg("登录成功");
    }

    @ApiOperation(value = "个人信息查询")
    @PostMapping("/personal")
    @SaCheckLogin
    public Result wechatPersonal() {
        String openId = StpUtil.getLoginIdAsString();
        WechatInfoDTO info = wechatClientService.getWechatInfo(openId);
        if (info.getPhone() != null) {
            info.setPhone(DesensitizedUtil.mobilePhone(info.getPhone()));
        }
        return Result.success(info).msg("查询完成");
    }


    @ApiOperation(value = "修改个人信息")
    @PostMapping("/editInfo")
    @SaCheckLogin
    public Result editInfo(EditWechatInfoVO editWechatInfoVO) {
        String openId = StpUtil.getLoginIdAsString();
        wechatClientService.editWechatInfo(openId, editWechatInfoVO);
        return Result.success().msg("修改完成");
    }

}
